package com.stone.back.dish.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.dish.entity.BfWeekmenu;
import com.stone.back.dish.entity.BfWeekmenuCombo;
import com.stone.back.dish.service.IWeekMenuService;
import com.stone.back.user.entity.BfUser;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;
import com.stone.common.util.DateUtil;

/**
 * 周菜单管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/backWeekMenuCtl")
public class WeekMenuController extends BaseController {
	@Autowired
	private IWeekMenuService weekMenuService;
	
	/**
	 * 查询周菜单分页信息
	 * @param pageNumber	页数
	 * @param startDate		销售日期
	 * @param endDate		销售日期
	 * @return
	 */
	@RequestMapping("/select")
    public @ResponseBody Page selectWeekMenuPage(String pageNumber, String startDate, String endDate, HttpServletRequest request){
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "1";
		}
		Page page = weekMenuService.selectWeekMenuPage(Integer.parseInt(pageNumber), startDate, endDate);
		return page;
	}
	
	/**
	 * 增加周菜单
	 * @param weekmenuid	菜单Id
	 * @param sellweek		销售日期
	 * @param remark		备注
	 * @param comboIds		菜单Ids
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/operate")
    public ModelAndView operateWeekMenu(String weekmenuid, Float price, String sellweek, String remark, String comboId1, String comboId2, String comboId3, 
    		String comboId4, String comboId5, HttpServletRequest request) throws IOException {
		ModelAndView mv = new ModelAndView("/background/weekmenu/weekmenulist");
		BfWeekmenu weekMenu = new BfWeekmenu();
		if(StringUtils.isNotBlank(weekmenuid)) {
			weekMenu = this.weekMenuService.selectWeekMenuDetail(Integer.parseInt(weekmenuid));
		}
		//销售日期
		Date sellweekDate = DateUtil.strFormatDate(DateUtil.DATE_FORMAT, sellweek);
		weekMenu.setSellweek(sellweekDate);
		weekMenu.setPrice(price);
		//备注
		weekMenu.setRemark(remark);
		//创建信息
		weekMenu.setCreatedate(new Date());
		BfUser currentUser = this.getBackCurrentUser(request);
		weekMenu.setCreateuserid(currentUser.getUserid());
		if(StringUtils.isBlank(weekmenuid)) {
			weekMenuService.addWeekMenu(weekMenu);
		} else {
			//修改周菜单
			weekMenuService.updateWeekMenu(weekMenu);
			//删除原周菜单和套餐关系
			weekMenuService.deleteWeekMenuDish(weekMenu.getWeekmenuid());
		}
		//增加菜单和套餐关系
		String[] comboIdArr = new String[5];
		comboIdArr[0] = comboId1;
		comboIdArr[1] = comboId2;
		comboIdArr[2] = comboId3;
		comboIdArr[3] = comboId4;
		comboIdArr[4] = comboId5;
		for (int i = 0; i < comboIdArr.length; i++) {
			BfWeekmenuCombo weekmenuCombo = new BfWeekmenuCombo();
			weekmenuCombo.setWeekmenuid(weekMenu.getWeekmenuid());
			weekmenuCombo.setComboid(Integer.parseInt(comboIdArr[i]));
			weekmenuCombo.setSelldate(i+1);
			weekMenuService.addWeekMenuCombo(weekmenuCombo);
		}
		return mv;
	}
	
	/**
	 * 获取周菜单信息去修改
	 * @param weekMenu	周菜单信息
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/toUpdate")
    public ModelAndView toUpdateWeekMenu(int weekMenuId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/weekmenu/weekmenuadd");
		BfWeekmenu weekMenu = this.weekMenuService.selectWeekMenuDetail(weekMenuId);
		List<BfWeekmenuCombo> weekmenuComboList = this.weekMenuService.selectWeekMenuComboDetail(weekMenuId);
		mv.addObject("weekMenu", weekMenu);
		mv.addObject("weekmenuComboList", weekmenuComboList);
		return mv;
	}
	
	/**
	 * 查询周菜单详情
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@RequestMapping("/detail")
    public ModelAndView selectWeekMenuDetail(int weekMenuId, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/background/weekmenu/weekmenudetail");
		BfWeekmenu weekMenu = weekMenuService.selectWeekMenuDetail(weekMenuId);
		List<BfWeekmenuCombo> weekMenuList = this.weekMenuService.selectWeekMenuComboDetail(weekMenuId);
		mv.addObject("weekMenu", weekMenu);
		mv.addObject("weekmenuComboList", weekMenuList);
		return mv;
	}
	
	/**
	 * 删除周菜单
	 * @param weekMenuId	周菜单Id
	 * @return
	 */
	@RequestMapping("/delete")
    public @ResponseBody boolean deleteWeekMenu(int weekMenuId, HttpServletRequest request){
		BfWeekmenu weekMenu = weekMenuService.selectWeekMenuDetail(weekMenuId);
		weekMenuService.deleteWeekMenu(weekMenuId);
		//删除原周菜单和套餐关系
		weekMenuService.deleteWeekMenuDish(weekMenuId);
		return true;
	}
}
